
#include "CUserShop.h"
#include "../clan/CClanMgr.h"
#include "../mainctrl.h"

int CUserShop::Init()
{
    _astBuyBackItems.Clear();  //重新登录时,清空回购列表
	return 0;
}

void CUserShop::Clear()
{
    _uiGid = 0;
    _astBuyBackItems.Clear();
    _stMysticalShop.Clear();
    _stClanShop.Clear();
    _iClearCrystalShopTime = 0;
    _astCrystalItems.Clear();
}

int CUserShop::AddBuyBackItem(CShopBuyBackItem& stBuyBackItem)
{
    //清理背包
    if (_astBuyBackItems.Size() >= _astBuyBackItems.GetCount())
    {
        _astBuyBackItems.ShiftDelOneItem(0); //清理掉最前面那个
    }

    _astBuyBackItems.AddOneItem(stBuyBackItem);
    return 0;
}

int CUserShop::TakeoutBuyBackItem(unsigned short usPos, CShopBuyBackItem& stBuyBackItem)
{
    if (usPos >= _astBuyBackItems.Size())
    {
        return ERROR_SHOP_NO_BUYBACK_ITEM;
    }

    stBuyBackItem = _astBuyBackItems[usPos];
    _astBuyBackItems.ShiftDelOneItem(usPos);

    return 0;
}

int CUserShop::GetBuyBackInfo(unsigned short usPos, CShopBuyBackItem& stBuyBackItem)
{
    if (usPos >= _astBuyBackItems.Size())
    {
        return ERROR_SHOP_NO_BUYBACK_ITEM;
    }

    stBuyBackItem = _astBuyBackItems[usPos];
    return 0;
}


//金币恢复魔咒的价格
int CMysticalShop::GetGoldRefreshCost ()
{
    if (_ucGoldRefreshCnt < 1)
    {
        return 50;
    }

    if (_ucGoldRefreshCnt < 5)
    {
        return 100;
    }

    if (_ucGoldRefreshCnt < 10)
    {
        return 200;
    }

    return 300;
}

//两种情况下更新魔杖数量: a)用户开启神秘商店功能时, b)用户请求商店信息时
void CMysticalShop::RenewCaneNum (int iCaneCDTime, int iNowTime)
{
    if (iNowTime >= _iRenewCaneTime)
    {
        int iElapseTime = iNowTime - _iRenewCaneTime;
        _ucCaneCnt += ((iElapseTime / iCaneCDTime) + 1);
        if (_ucCaneCnt > MYSTICAL_SHOP_CANE_MAXNUM)
        {
            _ucCaneCnt = MYSTICAL_SHOP_CANE_MAXNUM;
        }

        if (iNowTime >= _iClearCntTime)
        {
            _ucGoldRefreshCnt = 0;
            _iClearCntTime = iNowTime + (MYSTICAL_CLEAR_REFRESH_TIME - iElapseTime % MYSTICAL_CLEAR_REFRESH_TIME);
        }

        _iRenewCaneTime = iNowTime + (iCaneCDTime - iElapseTime %iCaneCDTime);
    }
}

int CMysticalShop::GetShopIemList(T_SHOP_ITEM_LIST & stShopItemList)
{
    stShopItemList.Clear();
    for (int i = 0; i < _astShopItem.Size(); i++)
    {
        int iRet = stShopItemList.AddOneItem(_astShopItem[i]);
        if (iRet < 0)
        {
            return iRet;
        }
    }
    return 0;
}

int CMysticalShop::GetShopItem (unsigned char ucPos, CShopItemConfig & stShopItem)
{
    if (ucPos > _astShopItem.Size())
    {
        return ERROR_SHOP_NO_SELL_ITEM;
    }

    stShopItem = _astShopItem[ucPos];
    return 0;
}

bool CMysticalShop::HasSameItem (CDropItemData & stItem)
{
    for (int i = 0; i < _astShopItem.Size(); i++)
    {        
        if (stItem._ucItemType == _astShopItem[i]._ucItemType
            && stItem._usItemConfigID == _astShopItem[i]._usItemConfigID)
        {
            return true;
        }
    }

    return false;
}


int CUserShop::AddClanShopItem(unsigned char ucPos)
{
    CClanEntry stClanEntry;
    int iRet = CMainCtrl::m_pstMain->m_stClanMgr.GetClanEntry(_stClanShop._uiClanId, stClanEntry);
    if (iRet)
    {
        CT_ERROR(("%d,%d", _stClanShop._uiClanId, iRet));
        return iRet;
    }
    
    _stClanShop._astBuyItems.AddOneItem(ucPos);
    _stClanShop._iLastBuyTime = CMainCtrl::m_pstMain->m_iNow;

    return 0;
}

int CUserShop::GetClanShopList (T_SHOP_ITEM_LIST & stShopItemList)
{
    stShopItemList.Clear();
    
    int iIndex = CMainCtrl::m_pstMain->m_stClanMgr.GetClanIndexByClanId (_stClanShop._uiClanId);
    if (iIndex < 0)
    {
        CT_ERROR(("%d,%d", _stClanShop._uiClanId, iIndex));
        return ERROR_CLAN_NOT_EXIST;
    }

    CClanEntry & stClanEntry = CMainCtrl::m_pstMain->m_stClanMgr.GetClanEntry(iIndex);
    CShopItemConfig stConfigData;
    for (int i = 0; i < stClanEntry._stClanShop._astItems.Size(); i++)
    {
        stConfigData = stClanEntry._stClanShop._astItems[i];
        for (int k = 0; k < _stClanShop._astBuyItems.Size(); ++k)
        {
            if (i == _stClanShop._astBuyItems[k])
            {
                stConfigData._usExchangeItemID = SHOP_ITEM_SALE_OUT;
                break;
            }
        }

        stShopItemList.AddOneItem(stConfigData);
    }

    return 0;
}


